using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using WebCrawlerTools.form;
using System.Windows.Forms;
using WebCrawlerTools.data;

namespace WebCrawlerTools.page
{
    public class Page
    {
        private string name = "";
        private string url = "";
        private string template = "";
        private PPageMove loop;
        private PPageType type;

        public String Url
        {
            get
            {
                return url;
            }
            set
            {
                url = value;
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public String Template
        {
            get{return template;}
            set{ template = value;}
        }

        public PPageType PageType
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }
        public PPageMove Move
        {
            get
            {
                return loop;
            }
            set
            {
                loop = value;
            }
        }

        public static PHomePage CreateHomePage()
        {
            PHomePage homePage = new PHomePage();
            return homePage;
        }
    }

    public class PListPage : Page
    {
        public PListPage()
        {
            Name = "ListPage";
            PageType = PPageType.LIST_PAGE; 
        }
        private PDetailPage detailPage = new PDetailPage();
    }

    public class PDetailPage : Page
    {
        public PDetailPage()
        {
            Name = "DetailPage";
            PageType = PPageType.DETAIL_PAGE; 
        }
    }

    public enum PPageType
    {
        LIST_PAGE,
        DETAIL_PAGE,
        SEARCH_PAGE,
        HOME_PAGE,
    }

    public class PSearchPage : Page
    {
    
        public PSearchPage()
        {
            Name = "SearchPage";
            PageType = PPageType.SEARCH_PAGE;
            forms = new ArrayList();
        }

        private ArrayList forms;
        public ArrayList Forms
        {
            get
            {
                return forms;
            }
            set
            {
                forms = value;
            }
        }
    
        private HForm GetFormByName(string formName)
        {
            foreach (HForm hf in forms)
            {
                if (hf.Name == formName)
                {
                    return hf;
                }
            }
            return null;
        }

        public void UpdateData(bool isStore, HtmlElementCollection doc)
        {
            if (isStore)
            {
                forms.Clear();
                foreach (HtmlElement ele in doc)
                {
                    HForm hf = HElementFactory.createForm(ele);
                    if (hf.Elements.Count > 0)
                    {
                        forms.Add(hf);
                    }
                }
            }
            else
            {
                foreach (HtmlElement ele in doc)
                {
                    HForm hf = GetFormByName(ele.Name);
                    if (hf != null)
                    {
                        HElementFactory.setForm(ele, hf);
                        Log.Inst().Write(hf.ToString());
                    }
                }
            }
        }

        internal void Input(HtmlElementCollection doc)
        {
            UpdateData(false, doc);
        }
    }

    public class PHomePage : Page
    {
        private PSearchPage searchPage = new PSearchPage();
        private PListPage listPage = new PListPage();
        private PDetailPage detailPage = new PDetailPage();
        public PHomePage()
        {
            Name = "HomePage";
            PageType = PPageType.HOME_PAGE; 
        }
    
        public PListPage ListPage
        {
            get
            {
                return listPage;
            }
            set
            {
                listPage = value;
            }
        }

        public PDetailPage DetailPage
        {
            get
            {
                return detailPage;
            }
            set
            {
                detailPage = value;
            }
        }

        public PSearchPage SearchPage
        {
            get
            {
                return searchPage;
            }
            set
            {
                searchPage = value;
            }
        }
    }
}
